<?php

require FCPATH . '/application/controllers/backend/backend.php';

class Service extends Backend {

    public function __construct() {
	parent::__construct();
	$this->load->model('service_model', 'model');
    }

    public function index() {
	$this->load->config('global');
	$this->set_js('CATEGORIES',json_encode($this->config->item('service_categories')));
	$this->load_js('backend_service');
	$this->rend('service', array(
	    'l_position' => array(array('name' => '服务'))
	));
    }

    public function records() {
	$page = intval($this->input->get('page'));
	$page_size = intval($this->input->get('pageSize'));
	$page = $page ? $page : 1;
	$page_size = $page_size ? $page_size : 10;
	$offset = $page_size * ($page - 1);

	$conditions = array();
	$id = intval($this->input->get('id'));
	if (!empty($id)) {
	    $conditions[] = array('where', 'id', $id);
	}

	$category = intval($this->input->get('category'));
	if (!empty($category)) {
	    $conditions[] = array('where', 'category', $category);
	}
	
	$menu = trim($this->input->get('menu'));
	if (!empty($menu)) {
	    $conditions[] = array('or_like', 'menu_zh', $menu);
	    $conditions[] = array('or_like', 'menu_fr', $menu);
	    $conditions[] = array('or_like', 'menu_en', $menu);
	}

	$title = trim($this->input->get('title'));
	if (!empty($title)) {
	    $conditions[] = array('or_like', 'title_zh', $title);
	    $conditions[] = array('or_like', 'title_fr', $title);
	    $conditions[] = array('or_like', 'title_en', $title);
	}

	$start = $this->input->get('start');
	if (!empty($start)) {
	    $conditions[] = array('where', 'created_at >=', $start);
	}

	$end = $this->input->get('end');
	if (!empty($end)) {
	    $conditions[] = array('where', 'created_at <=', $end);
	}

	$order_fields = array( 'category', 'menu_zh', 'menu_fr', 'menu_en', 'title_zh', 'title_fr', 'title_en', 'created_at');
	$order_field = intval($this->input->get('order_field'));
	$order_type = intval($this->input->get('order_type'));
	$orders = array();
	if ($order_field >= 0 && $order_field < count($order_fields)) {
	    $orders = array(
		array($order_fields[$order_field], $order_type == 1 ? 'asc' : 'desc')
	    );
	}
	$this->json_back('XT001', '', array(
	    'total' => $this->model->total($conditions),
	    'records' => $this->model->records(array(), $conditions, $orders, $page_size, $offset)
	));
    }

    public function show() {
	$this->load->model('slide_show_model');

	$id = intval($this->uri->segment(4));

	$slide_show_conditions = array(
	    array('where','type',3),
	    array('where', 'parent', $id)
	);

	if (!($about = $this->model->find($id))) {
	    die('您所访问的资源不存在');
	}

	$data = array(
	    'service' => $about,
	    'slide_shows' => $this->slide_show_model->records(array(), $slide_show_conditions),
	    'l_position' => array(
		array('name' => '服务', 'path' => '/backend/service/index'),
		array('name' => $about->menu_zh . ' / ' . $about->menu_fr . ' / ' . $about->menu_en)
	    )
	);

	$this->load_js('backend_service_show');
	$this->load_css('backend_service_show');
	$this->rend('service_show', $data);
    }

    public function add() {
	$this->load_css('backend_service_form');
	$this->load_js('backend_service_form');
	$this->load->config('global');
	$this->rend('service_add', array(
	    'l_position' => array(
		array('name' => '服务', 'path' => '/backend/service/index'),
		array('name' => '添加子栏目')
	    ),
	    'categories' => $this->config->item('service_categories')
	));
    }

    public function edit() {
	$id = intval($this->uri->segment(4));
	$this->load->config('global');
	$data = array(
	    'service' => $this->model->find($id),
	    'l_position' => array(
		array('name' => '关于我们', 'path' => '/backend/service/index'),
		array('name' => '编辑子栏目')
	    ),
	    'categories' => $this->config->item('service_categories')
	);
	$this->load_css('backend_service_form');
	$this->load_js('backend_service_form');
	$this->rend('service_edit', $data);
    }

    public function insert() {
	$data = array(
	    'category' => intval($this->input->post('category')),
	    'menu_zh' => trim($this->input->post('menu_zh')),
	    'menu_fr' => trim($this->input->post('menu_fr')),
	    'menu_en' => trim($this->input->post('menu_en')),
	    'title_zh' => trim($this->input->post('title_zh')),
	    'title_fr' => trim($this->input->post('title_fr')),
	    'title_en' => trim($this->input->post('title_en')),
	    'content_zh' => $this->input->post('content_zh'),
	    'content_fr' => $this->input->post('content_fr'),
	    'content_en' => $this->input->post('content_en'),
	);
	$this->validate($data);
	if (($id = $this->model->insert($data)) > 0) {
	    $this->json_back('XT001', '添加成功', array('id' => $id));
	}
	$this->json_back('XT002', '数据库或SQL语句有误。');
    }

    public function update() {
	$id = intval($this->input->post('id'));
	$data = array(
	    'category' => intval($this->input->post('category')),
	    'menu_zh' => trim($this->input->post('menu_zh')),
	    'menu_fr' => trim($this->input->post('menu_fr')),
	    'menu_en' => trim($this->input->post('menu_en')),
	    'title_zh' => trim($this->input->post('title_zh')),
	    'title_fr' => trim($this->input->post('title_fr')),
	    'title_en' => trim($this->input->post('title_en')),
	    'content_zh' => $this->input->post('content_zh'),
	    'content_fr' => $this->input->post('content_fr'),
	    'content_en' => $this->input->post('content_en'),
	);
	$this->validate($data);
	if ($this->model->update($id, $data)) {
	    $this->json_back('XT001', '更新成功', array('id' => $id));
	}
	$this->json_back('XT002', '数据库或SQL语句有误。');
    }

    public function delete() {
	$id = intval($this->input->get('id'));
	if ($this->model->delete($id)) {
	    $this->json_back('XT001', '删除成功。');
	}
	$this->json_back('XT002', '数据库或SQL语句有误。');
    }

    private function validate($data) {
	if (empty($data['category'])) {
	    $this->json_back('XT002', '请选择分类');
	}
	
	if (empty($data['menu_zh'])) {
	    $this->json_back('XT002', '请填写菜单（中文）');
	}

	if (empty($data['menu_fr'])) {
	    $this->json_back('XT002', '请填写菜单（法文）');
	}

	if (empty($data['menu_en'])) {
	    $this->json_back('XT002', '请填写菜单（英文）');
	}

	if (empty($data['title_zh'])) {
	    $this->json_back('XT002', '请填写标题（中文）');
	}

	if (empty($data['title_fr'])) {
	    $this->json_back('XT002', '请填写标题（法文）');
	}

	if (empty($data['title_en'])) {
	    $this->json_back('XT002', '请填写标题（英文）');
	}

	if (empty($data['content_zh'])) {
	    $this->json_back('XT002', '请填写内容（中文）');
	}

	if (empty($data['content_fr'])) {
	    $this->json_back('XT002', '请填写内容（法文）');
	}

	if (empty($data['title_en'])) {
	    $this->json_back('XT002', '请填写内容（英文）');
	}
    }

}
